// Author: Bjorn Schobben

#include "ColorButton.h"

ColorButton::ColorButton(QWidget *parent): QPushButton(parent)
{
	Initialize();
}

ColorButton::~ColorButton()
{
}

void ColorButton::Initialize()
{
	// Set colorbutton's color to match internal color
	SetColorValue(GetColorValue());

	// Connect clicked signal
	QObject::connect(this, SIGNAL(clicked()), this, SLOT(onClicked()));
}


//Set a color value for the button
void ColorButton::SetColorValue(int newColor)
{
	Color::SetColorValue(newColor);

	// Convert color to an icon:
	QPixmap pixmap(PIXMAPSIZE, PIXMAPSIZE);

	if (newColor != NOCOLOR)
	{
		// Fill the pixmap with the selected color
		pixmap.fill(QColor(GetRed(), GetGreen(), GetBlue()));
	}
	else
	{
		// If this is an erased color, load the eraser icon
		pixmap.load("icons/eraser.png");
	}

	QIcon icon(pixmap);

	// Set the icon corresponding to the color
	setIcon(icon);
}

//Signal sending out that the button is clicked together with the info the button holds
void ColorButton::onClicked()
{
	emit clicked(this, *this);
}